package main


import "fmt"
import ."api"
import "io/ioutil"
import "state"


func main() {

	data, err := ioutil.ReadFile("test.luac")
	if err != nil {
		panic(err)
	}

	ls := state.New()
	ls.Register("print", print)		// 注冊print函數

	ls.Load(data, "chunk", "b")
	ls.Call(0, 0)

}


func print(ls LuaState) int {

	nArgs := ls.GetTop()  // 此时栈里存放的都是参数

	for i := 1; i <= nArgs; i++ {

		if ls.IsBoolean(i) {

			fmt.Printf("%t", ls.ToBoolean(i))

		} else if ls.IsString(i) {

			fmt.Print(ls.ToString(i))

		} else {

			fmt.Print(ls.TypeName(ls.Type(i)))

		}

		if i < nArgs {
			fmt.Printf("\t")
		}

	}

	fmt.Println()

	return 0

}